package pl.javarsovia.javarsovia4android.sync;

import android.content.Context;
import android.util.Log;
import pl.javarsovia.javarsovia4android.domain.ConferenceProvider;
import pl.javarsovia.javarsovia4android.util.ImageManager;

/**
 * @author mateusz.grzechocinski@gmail.com
 * @since 2010-06-20
 */
public class SyncingThread extends Thread {
	private static final String LOG_TAG = "SyncingThread";

	public interface OnSyncStatusChangedListener {
		public void onSyncEnded();

		public void onSyncError(String message);
	}

	private OnSyncStatusChangedListener listener;
	private Context androidContext;

	public SyncingThread(OnSyncStatusChangedListener listener, Context androidContext) {
		this.listener = listener;
		this.androidContext = androidContext;
	}

	@Override
	public void run() {
		try {
			ConferenceProvider.updateConferenceFromWeb(androidContext);
			ImageManager.getInstance().downloadSpeakersPhotos(ConferenceProvider.getConference().getSpeakers());
			ImageManager.getInstance().loadAllPhotos();
			listener.onSyncEnded();
		} catch (Exception e) {
			Log.d(LOG_TAG, "Could not sync!", e);
			listener.onSyncError(e.getMessage());
		}
	}
}
